/**
 * 
 */
package vue.popup;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import model.solver.ImportSolverParameter;

/**
 * A {@link IGDialogParameter} for {@link ImportSolverParameter}.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class IGImportDialogParameter extends IGDialogParameter implements MouseListener {

	private static final long		serialVersionUID	= 1L;
	public static final String		CNF					= "CNF";
	public static final String		PARAMETER_PREFIX	= "Parameter ";
	public static final String		INFORMATION			= "Select the parameter and click on the one that is use \nfor the CNF";

	public static final Border		CNF_BORDER			= BorderFactory.createTitledBorder(BorderFactory.createMatteBorder(1, 1, 1, 1,
																Color.BLUE), CNF);

	private ImportSolverParameter	param;

	private JPanel					timeoutPanel;
	private JLabel					timeoutDescritpion;

	private List<JTextField>		paremeters			= new ArrayList<JTextField>();
	private List<JPanel>			parametersPanel		= new ArrayList<JPanel>();

	/**
	 * Create a {@link IGImportDialogParameter} .
	 * 
	 * @param param
	 *            {@link ImportSolverParameter} of the solver.
	 */
	public IGImportDialogParameter(ImportSolverParameter param) {

		this.param = param;

		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		// info
		add(new JLabel(INFORMATION));

		// timeout
		timeoutPanel = new JPanel(new GridLayout(1, 2));

		// timeoutButton = new JButton(TIMEOUT_BUTTON);
		// timeoutButton.addActionListener(this);
		timeoutDescritpion = new JLabel(TIMEOUT_DESCRIPTION);
		timeoutPanel.add(timeoutDescritpion);
		timeoutPanel.add(timeoutField);
		// panel.add(timeoutButton);
		// panel.setBorder(BorderFactory.createTitledBorder(TIMEOUT_DESCRIPTION));

		add(timeoutPanel);

		// param
		for (int i = 0 ; i < param.getNbMaxParameter() ; i++)
		{
			JPanel panel = new JPanel(new GridLayout(1, 2));
			panel.add(new JLabel(PARAMETER_PREFIX + i));

			JTextField val = new JTextField();
			if (i == param.getCnfposition())
			{
				val.setText(CNF);
				panel.setBorder(CNF_BORDER);
			}
			else
				val.setText(param.getParams().get(i));

			paremeters.add(val);
			panel.add(val);
			add(panel);
			panel.addMouseListener(this);
			parametersPanel.add(panel);
		}
		// empeche d'editer et selectionner le chemin de l'executable
		// paremeters.get(0).setEditable(false);
		paremeters.get(0).removeMouseListener(this);

		refresh();
	}


	@Override
	public boolean checkAndSetParameter() {
		boolean good = true;

		// timeout
		String timeout = timeoutField.getText();
		if (timeout.isEmpty())
		{
			param.setTimeOut(ImportSolverParameter.NO_DELAY);
			timeoutPanel.setBorder(null);
		}
		else
			try
			{
				param.setTimeOut(Integer.parseInt(timeout));
				timeoutPanel.setBorder(null);
			} catch (Exception e)
			{
				timeoutPanel.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
				good = false;
			}

		List<String> list = param.getParams();
		for (int i = 0 ; i < param.getNbMaxParameter() ; i++)
			list.set(i, paremeters.get(i).getText());

		// verifie l"executable n'est pas vide
		if (paremeters.get(0).getText().isEmpty())
		{
			good = false;
			parametersPanel.get(0).setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.RED));
		}
		else
			parametersPanel.get(0).setBorder(null);

		return good;
	}

	@Override
	public void refresh() {
		timeoutField.setText("" + param.getTimeOut());

		for (int i = 0 ; i < param.getNbMaxParameter() ; i++)
		{// get les parametre
			JTextField val = paremeters.get(i);
			if (i == param.getCnfposition())
			{
				val.setText(CNF);
				parametersPanel.get(i).setBorder(CNF_BORDER);
			}
			else
			{
				val.setText(param.getParams().get(i));
				parametersPanel.get(i).setBorder(null);
			}
		}
	}


	@Override
	public void mouseClicked(MouseEvent arg0) {
		for (int i = 1 ; i < parametersPanel.size() ; i++)
			if (arg0.getSource() == parametersPanel.get(i))
			{// selectionne le panel pour etre position cnf par default
				// deselectionne l'ancien
				parametersPanel.get(param.getCnfposition()).setBorder(null);
				paremeters.get(param.getCnfposition()).setText("");

				// selectionne le nouveau
				paremeters.get(i).setText(CNF);
				parametersPanel.get(i).setBorder(CNF_BORDER);

				// maj
				param.setCnfposition(i);
				return;
			}
	}


	@Override
	public void mouseEntered(MouseEvent arg0) {
	}


	@Override
	public void mouseExited(MouseEvent arg0) {
	}


	@Override
	public void mousePressed(MouseEvent arg0) {
	}


	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

}
